package com.bytz.modules.cms.domain.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

public class DecimalFormatUtil {

    public static final String DEFAULT_MONEY_PATTERN = "#,###.00";

    public static final String TEN_THOUSAND_MONEY_PATTERN = "#,###万元";

    public static final String DEFAULT_PERCENT_PATTERN = "#.##%";

    public static final String DEFAULT_PERCENT_PATTERN_WITHOUT_SYMBOL = "#.##";

    public static final String DEFAULT_C4C_MONEY_PATTERN = "####.000000";

    /**
     * 格式化价格
     */
    public static String formatMoney(BigDecimal source) {
        return format(source, DEFAULT_MONEY_PATTERN);
    }

    /**
     * 格式化价格为万元
     */
    public static String formatTenThousandMoney(BigDecimal source) {
        if (source.compareTo(BigDecimal.valueOf(10000)) < 0) {
            return "不足1万元";
        }
        BigDecimal divide = source.divide(BigDecimal.valueOf(10000), 0, RoundingMode.HALF_UP);
        return format(divide, TEN_THOUSAND_MONEY_PATTERN);
    }

    /**
     * 格式化百分比
     */
    public static String formatPercent(BigDecimal source) {
        return format(source, DEFAULT_PERCENT_PATTERN);
    }

    /**
     * 格式化百分比，不带百分号
     */
    public static String formatPercentWithoutSymbol(BigDecimal source) {
        return format(source.multiply(BigDecimal.valueOf(100)), DEFAULT_PERCENT_PATTERN_WITHOUT_SYMBOL);
    }

    public static String formatForC4c(BigDecimal source) {
        return format(source, DEFAULT_C4C_MONEY_PATTERN);
    }

    public static String format(BigDecimal source, String pattern) {
        DecimalFormat df = new DecimalFormat(pattern);
        return df.format(source);
    }

}
